package network.tcp.tmp;

import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @program: baseModule
 * @description:
 * @author: zhanghz001
 * @create: 2020-10-26 16:53
 **/
public class TcpServer {
    public static void main(String[] args) {
        server();
    }

    private static void server() {
        try (ServerSocket server = new ServerSocket(9000);
             Socket accept = server.accept();
             InputStream is = accept.getInputStream();
             BufferedWriter os = new BufferedWriter(new OutputStreamWriter(accept.getOutputStream()));
        ) {

            //接收消息
            byte[] bytes = new byte[1024];
            int len;
            while ((len = is.read(bytes)) != -1) {
                System.out.println(new String(bytes, 0, len));
            }

            //发送消息
            os.write("服务端发送消息1");
            os.newLine();
            os.flush();
            os.write("服务端发送消息222");
            os.newLine();
            os.flush();
            os.write("服务端发送消息333");
            os.newLine();
            os.flush();
            accept.shutdownOutput();
            // accept.shutdownOutput();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
    }
}
